
import '../model/home_model.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;
const String homeUrl = "https://gitee.com/jsjxsy/flutter_trip/raw/master/json/home.json";
class HomeDao{

  static Future<HomeModel> fetch() async {
    Uri uri = Uri.parse(homeUrl);
    var response = await http.get(uri);
      if(response.statusCode == 200){
        print("response: $response");
        convert.Utf8Decoder utf8decoder = const convert.Utf8Decoder();
        var jsonResponse = convert.jsonDecode(utf8decoder.convert(response.bodyBytes)) as Map<String, dynamic>;
        return HomeModel.fromJson(jsonResponse);
      }else{
        throw Exception("Failed to load home_page.json");
      }
  }
}

class UserAgentClient extends http.BaseClient {
  final String userAgent;
  final http.Client _inner;

  UserAgentClient(this.userAgent, this._inner);

  Future<http.StreamedResponse> send(http.BaseRequest request) {
    request.headers['user-agent'] = userAgent;
    return _inner.send(request);
  }

}